<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Module_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_all_modules(){
        
	$query = $this->db->query('SELECT id, module_name, module_type FROM modules');

	foreach ($query->result() as $row)
	    $tab_modules[] = array('id' => $row->id, 'module_name' => $row->module_name, 'module_type' => $row->module_type);

	return $tab_modules;
    }

    function get_module_infos($module_id){
        
	$query = $this->db->query('SELECT module_name, module_type FROM module WHERE id='.$module_id);

	foreach ($query->result() as $row)
	    return array('id' => $row->id, 'module_name' => $row->module_name, 'module_type' => $row->module_type);
    }

    function add_module($teacher_id, $module_name, $module_type){

    	$data = array(
          'teacher_id' => $teacher_id,
          'module_name' => $module_name,
          'module_type' => $module_type
        );

	$this->db->insert('courses', $data); 

	if($this->db->affected_rows() > 0)
	    return 'Vous venez d'ajouter un module';
    }

    function delete_module($teacher_id, $module_id){

    	$query = $this->db->query('SELECT c.id FROM teachers t, module m
	       	 			  WHERE c.teacher_id=t.id and t.id='.$teacher_id.' and m.id='.$module_id);

	foreach ($query->result() as $row)
	    $result = 1;

	if(!empty($result)){	
            $query = 'DELETE FROM modules WHERE id='.$module_id;
	    $this->db->query($query);

	    if($this->db->affected_rows() > 0)
	        return 'Vous venez de supprimer un cours';
	}
    }
}